<?php
require_once(APPLICATION_PATH . '/../library/Exts/Services/facebook/fb.class.php');
class Exts_Views_Helpers_Facebook extends Zend_View_Helper_Abstract {

	private $_url;
	private $_user;
	public $showLogin = false;
	public function facebook() {
		
//		echo '<script src="http://connect.facebook.net/en_US/all.js#appId=122770627790747&amp;xfbml=1"></script>';

		$this->_url = 'http://';
		
		if( strpos($_SERVER['SERVER_NAME'],'www' ) === false ){
			$this->_url .= 'www.';
		}
		$this->_url .= $_SERVER['SERVER_NAME'];
		if( isset($_SERVER['REQUEST_URI'])){
			$this->_url .= $_SERVER['REQUEST_URI'];
		}
		if( $this->_url[strlen($this->_url)-1] == '/' ){
			$this->_url = substr($this->_url, 0, (strlen($this->_url)-1));
		}
		return $this;
	}


	public function requireLogin(){
		FbFacade::require_login(
			array('read-stream','publish-stream','email','share_item')
		);
		$id = FB::get_instance()->getUser();
		$this->_user = FB::get_instance()->getInfo( $id );
		$this->_user->picture = FB::get_instance()->getPicture($id);
		if( isset($this->_user->error->type )){
			$this->showLogin = true;
		}
	}

	public function showLoginButton(){
		return FB::get_instance()->showLoginButton();
	}

	public function getUser(){
		return $this->_user;
	}

	public function getPicture(){
		return $this->_user->picture;
	}
	public function getFullname(){
		return $this->_user->picture;
	}
	

	public function comments($url){
		/*if (APPLICATION_ENV != 'production') {
			return '';
		}*/

		return '<fb:comments href="'.$this->_url.'" num_posts="10" width="680"></fb:comments>';
	}

	public function iLiked( $url, $type='fxml' ) {
		if (APPLICATION_ENV != 'production') {
			//return '';
		}

		$url = urlencode( $this->_url );
		/*
		if( $type=='fxml') {
			return '<fb:like layout="button_count"></fb:like>';
		}
		 */

		return '
		<iframe src="http://www.facebook.com/plugins/like.php?href='.$url.'&amp;'
		.'layout=button_count&amp;show_faces=false&amp;width=450&amp;action=like'
		.'&amp;colorscheme=light&amp;height=30" scrolling="no" frameborder="0" '
		.'style="border:none; overflow:hidden; width:450px; height:30px;" '
		.'allowTransparency="true"></iframe>';

	}

}
